package com.atomic.test.example.utils;


import com.atomic.test.example.entity.Board;
import com.atomic.test.example.entity.Topic;
import com.atomic.test.example.entity.User;
import com.atomic.test.example.query.CreatePostDo;

/**
 * Created by DreamYao on 2017/1/26.
 */
public class CheckDataUtil {

    private CheckDataUtil(){}

    /**
     * 用户更新参数检查
     *
     * @param user
     * @return
     */
    public static Boolean updateUserCheck(User user) {
        String userName = user.getUserName();
        Integer userType = user.getUserType();
        Integer locked = user.getLocked();
        Integer credit = user.getCredit();
        if (user == null || userName == null || userName.equals("") ||
                userType == null || locked == null || credit == null) {
            return false;
        } else if (userType.intValue() != 2 || userType.intValue() != 1 ||
                locked.intValue() != 0 || locked.intValue() != 1 ||
                credit.intValue() < 0) {
            return false;
        }
        return true;
    }

    /**
     * 论坛创建参数检查
     *
     * @param board
     * @return
     */
    public static Boolean createBoardCheck(Board board) {
        String boardName = board.getBoardName();
        String boardDesc = board.getBoardDesc();
        if (board == null && boardName.equals("") && boardName == null
                && boardName.isEmpty() && boardDesc.equals("")
                && boardDesc.isEmpty() && boardDesc == null) {
            return false;
        }
        return true;
    }

    /**
     * 论坛更新参数检查
     *
     * @param board
     * @return
     */
    public static Boolean updateBoardCheck(Board board) {
        String boardName = board.getBoardName();
        String boardDesc = board.getBoardDesc();
        Integer id = board.getId();
        if (board == null && (boardName.equals("") && boardName == null
                && boardName.isEmpty()) || (boardDesc.equals("")
                && boardDesc.isEmpty() && boardDesc == null)
                && id == null && id > 0) {
            return false;
        }
        return true;
    }

    /**
     * 创建话题参数检查
     *
     * @param topic
     * @return
     */
    public static Boolean createTopicCheck(Topic topic) {
        if (topic == null) {
            return false;
        } else if (topic.getBoardId() != null && topic.getTopicReplies() != null && topic.getTopicTitle()!=null
                && !topic.getTopicTitle().equals("") && topic.getTopicViews() != null) {
            return true;
        }
        return false;
    }

    /**
     * 创建用户时用户名和密码中文检查
     * @param str
     * @return
     */
    public static Boolean isChinese(String str) {
        for (char c : str.toCharArray()) {
            if (c >= 0x4E00 && c <= 0x9FA5) {
                return false;
            }
        }
        if (str.toCharArray().length > 12 && str.toCharArray().length < 6) {
            return false;
        }
        return true;
    }

    /**
     * 创建或回复帖子参数检查
     * @param createPostDo
     * @return
     */
    public static Boolean insertPostCheck(CreatePostDo createPostDo) {
        if (createPostDo.getBoardId() == null || createPostDo.getTopicId() == null ||
                createPostDo.getPostType() == null || createPostDo.getPostTitle() == null ||
                createPostDo.getPostTitle().equals("") || createPostDo.getPostText() == null ||
                createPostDo.getPostText().equals("")) {
            return false;
        } else if (createPostDo.getBoardId() <= 0 || createPostDo.getTopicId() <= 0 ||
                createPostDo.getPostTitle().toCharArray().length > 50) {
            return false;
        } else if (createPostDo.getPostType() != 1 && createPostDo.getPostType() != 2) {
            return false;
        }
        return true;
    }
}
